/**
 * Created with IntelliJ IDEA.
 * Description: 双向链表
 * User: mac
 * Date: 2022-09-03
 * Time: 16:02
 */
class ListNode{
    public int val;
    public ListNode prev;
    public ListNode next;

    public ListNode(int val){
        this.val = val;
    }
}
public class DoubleLinkedList {
    ListNode head = new ListNode(-1);
    ListNode last;

    //打印
    public void display(){
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //链表长度
    public int size(){
        ListNode cur = this.head.next;
        int count = 0;
        while (cur != null){
            count++;
            cur  = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key节点是否在单链表中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if (head.next == null){
            head.next = node;
            node.prev = head;
            node.next = null;
            last = node;
        } else {
            node.next = head.next;
            head.next.prev = node;
            head.next = node;
            node.prev = head;
        }
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (this.head.next == null){
            head.next = node;
            node.prev = head;
            last = node;
        } else{
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    //删除第一次关键字为key的节点
    public void remove(int key){
        if (this.head.next == null){
            System.out.println("链表为空！");
            return;
        }
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                if (cur.prev == head){
                    head.next = cur.next;
                    if (cur.next == null){
                        last = null;
                        return;
                    }
                    cur.next.prev = head;
                    return;
                }
                if (cur.next == null){
                    last = last.prev;
                    last.next = null;
                    return;
                }
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;
            }
            cur = cur.next;
        }
        if (cur == null){
            System.out.println("未找到关键字为k的节点");
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (this.head.next == null){
            System.out.println("链表为空！");
            return;
        }
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                if (cur.prev == head){
                    head.next = cur.next;
                    if (cur.next == null){
                        last = null;
                        return;
                    }
                    cur.next.prev = head;
                }
                if (cur.next == null){
                    last = last.prev;
                    last.next = null;
                }
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
            }
            cur = cur.next;
        }
        if (cur == null){
            System.out.println("未找到关键字为k的节点");
        }
    }

    public ListNode search(int index){
        ListNode cur = this.head.next;

        while (index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入，第一个数据节点为0号下标
    public void addIndex(int index, int data){
        if (index < 0 || index > size()){
            System.out.println("插入位置不合法！");
            return;
        }
        ListNode node = new ListNode(data);
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }
        ListNode cur = search(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    //清空链表
    public void clear(){
        ListNode newHead = this.head.next;
        while (newHead != null){
            ListNode curNext = newHead.next;
            newHead.next = null;
            newHead.prev = null;
            newHead = curNext;
        }
        last = null;
        head.next = null;
    }
}
